package com.maodou.core.ui.components.textfield

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * 输入框样式配置
 */
object TextFieldStyles {
    
    /**
     * 默认样式
     */
    @Composable
    fun default(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = MaterialTheme.colorScheme.primary,
            unfocusedBorderColor = MaterialTheme.colorScheme.outline,
            focusedLabelColor = MaterialTheme.colorScheme.primary,
            unfocusedLabelColor = MaterialTheme.colorScheme.onSurfaceVariant,
            cursorColor = MaterialTheme.colorScheme.primary,
            errorBorderColor = MaterialTheme.colorScheme.error,
            errorLabelColor = MaterialTheme.colorScheme.error,
            errorCursorColor = MaterialTheme.colorScheme.error
        )
    }
    
    /**
     * 主要样式（强调色）
     */
    @Composable
    fun primary(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = MaterialTheme.colorScheme.primary,
            unfocusedBorderColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f),
            focusedLabelColor = MaterialTheme.colorScheme.primary,
            unfocusedLabelColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.7f),
            cursorColor = MaterialTheme.colorScheme.primary,
            focusedContainerColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.05f),
            unfocusedContainerColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.02f)
        )
    }
    
    /**
     * 成功样式（绿色主题）
     */
    @Composable
    fun success(): TextFieldColors {
        val successColor = Color(0xFF4CAF50)
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = successColor,
            unfocusedBorderColor = successColor.copy(alpha = 0.5f),
            focusedLabelColor = successColor,
            unfocusedLabelColor = successColor.copy(alpha = 0.7f),
            cursorColor = successColor,
            focusedContainerColor = successColor.copy(alpha = 0.05f),
            unfocusedContainerColor = successColor.copy(alpha = 0.02f)
        )
    }
    
    /**
     * 警告样式（橙色主题）
     */
    @Composable
    fun warning(): TextFieldColors {
        val warningColor = Color(0xFFFF9800)
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = warningColor,
            unfocusedBorderColor = warningColor.copy(alpha = 0.5f),
            focusedLabelColor = warningColor,
            unfocusedLabelColor = warningColor.copy(alpha = 0.7f),
            cursorColor = warningColor,
            focusedContainerColor = warningColor.copy(alpha = 0.05f),
            unfocusedContainerColor = warningColor.copy(alpha = 0.02f)
        )
    }
    
    /**
     * 错误样式（红色主题）
     */
    @Composable
    fun error(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = MaterialTheme.colorScheme.error,
            unfocusedBorderColor = MaterialTheme.colorScheme.error.copy(alpha = 0.5f),
            focusedLabelColor = MaterialTheme.colorScheme.error,
            unfocusedLabelColor = MaterialTheme.colorScheme.error.copy(alpha = 0.7f),
            cursorColor = MaterialTheme.colorScheme.error,
            focusedContainerColor = MaterialTheme.colorScheme.error.copy(alpha = 0.05f),
            unfocusedContainerColor = MaterialTheme.colorScheme.error.copy(alpha = 0.02f),
            errorBorderColor = MaterialTheme.colorScheme.error,
            errorLabelColor = MaterialTheme.colorScheme.error,
            errorCursorColor = MaterialTheme.colorScheme.error
        )
    }
    
    /**
     * 信息样式（蓝色主题）
     */
    @Composable
    fun info(): TextFieldColors {
        val infoColor = Color(0xFF2196F3)
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = infoColor,
            unfocusedBorderColor = infoColor.copy(alpha = 0.5f),
            focusedLabelColor = infoColor,
            unfocusedLabelColor = infoColor.copy(alpha = 0.7f),
            cursorColor = infoColor,
            focusedContainerColor = infoColor.copy(alpha = 0.05f),
            unfocusedContainerColor = infoColor.copy(alpha = 0.02f)
        )
    }
    
    /**
     * 暗色样式
     */
    @Composable
    fun dark(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = Color.White,
            unfocusedBorderColor = Color.White.copy(alpha = 0.5f),
            focusedLabelColor = Color.White,
            unfocusedLabelColor = Color.White.copy(alpha = 0.7f),
            cursorColor = Color.White,
            focusedContainerColor = Color.Black.copy(alpha = 0.8f),
            unfocusedContainerColor = Color.Black.copy(alpha = 0.6f),
            focusedTextColor = Color.White,
            unfocusedTextColor = Color.White.copy(alpha = 0.9f)
        )
    }
    
    /**
     * 透明样式
     */
    @Composable
    fun transparent(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = MaterialTheme.colorScheme.primary,
            unfocusedBorderColor = MaterialTheme.colorScheme.outline.copy(alpha = 0.3f),
            focusedLabelColor = MaterialTheme.colorScheme.primary,
            unfocusedLabelColor = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f),
            cursorColor = MaterialTheme.colorScheme.primary,
            focusedContainerColor = Color.Transparent,
            unfocusedContainerColor = Color.Transparent
        )
    }
    
    /**
     * 填充样式
     */
    @Composable
    fun filled(): TextFieldColors {
        return OutlinedTextFieldDefaults.colors(
            focusedBorderColor = Color.Transparent,
            unfocusedBorderColor = Color.Transparent,
            focusedLabelColor = MaterialTheme.colorScheme.primary,
            unfocusedLabelColor = MaterialTheme.colorScheme.onSurfaceVariant,
            cursorColor = MaterialTheme.colorScheme.primary,
            focusedContainerColor = MaterialTheme.colorScheme.surfaceVariant,
            unfocusedContainerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.7f)
        )
    }
}

/**
 * 输入框形状配置
 */
object TextFieldShapes {
    
    /**
     * 圆角矩形（小）
     */
    val small: Shape = RoundedCornerShape(4.dp)
    
    /**
     * 圆角矩形（中等）
     */
    val medium: Shape = RoundedCornerShape(8.dp)
    
    /**
     * 圆角矩形（大）
     */
    val large: Shape = RoundedCornerShape(12.dp)
    
    /**
     * 圆角矩形（超大）
     */
    val extraLarge: Shape = RoundedCornerShape(16.dp)
    
    /**
     * 胶囊形状
     */
    val pill: Shape = RoundedCornerShape(50)
    
    /**
     * 矩形（无圆角）
     */
    val rectangle: Shape = RoundedCornerShape(0.dp)
    
    /**
     * 顶部圆角
     */
    val topRounded: Shape = RoundedCornerShape(
        topStart = 8.dp,
        topEnd = 8.dp,
        bottomStart = 0.dp,
        bottomEnd = 0.dp
    )
    
    /**
     * 底部圆角
     */
    val bottomRounded: Shape = RoundedCornerShape(
        topStart = 0.dp,
        topEnd = 0.dp,
        bottomStart = 8.dp,
        bottomEnd = 8.dp
    )
}

/**
 * 输入框尺寸配置
 */
object TextFieldSizes {
    
    /**
     * 小尺寸
     */
    object Small {
        val height = 40.dp
        val padding = PaddingValues(horizontal = 12.dp, vertical = 8.dp)
        val textStyle = TextStyle(fontSize = 14.sp)
        val iconSize = 16.dp
    }
    
    /**
     * 中等尺寸（默认）
     */
    object Medium {
        val height = 48.dp
        val padding = PaddingValues(horizontal = 16.dp, vertical = 12.dp)
        val textStyle = TextStyle(fontSize = 16.sp)
        val iconSize = 20.dp
    }
    
    /**
     * 大尺寸
     */
    object Large {
        val height = 56.dp
        val padding = PaddingValues(horizontal = 20.dp, vertical = 16.dp)
        val textStyle = TextStyle(fontSize = 18.sp, fontWeight = FontWeight.Medium)
        val iconSize = 24.dp
    }
}

/**
 * 样式化的输入框组件
 */
@Composable
fun StyledTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    label: String? = null,
    placeholder: String? = null,
    style: TextFieldStyle = TextFieldStyle.Default,
    size: TextFieldSize = TextFieldSize.Medium,
    shape: Shape = TextFieldShapes.medium,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    singleLine: Boolean = true,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
    isError: Boolean = false,
    supportingText: String? = null
) {
    val colors = when (style) {
        TextFieldStyle.Default -> TextFieldStyles.default()
        TextFieldStyle.Primary -> TextFieldStyles.primary()
        TextFieldStyle.Success -> TextFieldStyles.success()
        TextFieldStyle.Warning -> TextFieldStyles.warning()
        TextFieldStyle.Error -> TextFieldStyles.error()
        TextFieldStyle.Info -> TextFieldStyles.info()
        TextFieldStyle.Dark -> TextFieldStyles.dark()
        TextFieldStyle.Transparent -> TextFieldStyles.transparent()
        TextFieldStyle.Filled -> TextFieldStyles.filled()
    }
    
    val textStyle = when (size) {
        TextFieldSize.Small -> TextFieldSizes.Small.textStyle
        TextFieldSize.Medium -> TextFieldSizes.Medium.textStyle
        TextFieldSize.Large -> TextFieldSizes.Large.textStyle
    }
    
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier.then(
            when (size) {
                TextFieldSize.Small -> Modifier.height(TextFieldSizes.Small.height)
                TextFieldSize.Medium -> Modifier.height(TextFieldSizes.Medium.height)
                TextFieldSize.Large -> Modifier.height(TextFieldSizes.Large.height)
            }
        ),
        label = label?.let { { Text(it) } },
        placeholder = placeholder?.let { { Text(it) } },
        supportingText = supportingText?.let { { Text(it) } },
        enabled = enabled,
        readOnly = readOnly,
        singleLine = singleLine,
        maxLines = maxLines,
        isError = isError,
        colors = colors,
        textStyle = textStyle,
        shape = shape
    )
}

/**
 * 输入框样式枚举
 */
enum class TextFieldStyle {
    Default,
    Primary,
    Success,
    Warning,
    Error,
    Info,
    Dark,
    Transparent,
    Filled
}

/**
 * 输入框尺寸枚举
 */
enum class TextFieldSize {
    Small,
    Medium,
    Large
}

/**
 * 带边框的输入框
 */
@Composable
fun BorderedTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    label: String? = null,
    placeholder: String? = null,
    borderColor: Color = MaterialTheme.colorScheme.outline,
    borderWidth: Dp = 1.dp,
    shape: Shape = TextFieldShapes.medium,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    singleLine: Boolean = true,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE
) {
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        label = label?.let { { Text(it) } },
        placeholder = placeholder?.let { { Text(it) } },
        enabled = enabled,
        readOnly = readOnly,
        singleLine = singleLine,
        maxLines = maxLines,
        shape = shape,
        colors = OutlinedTextFieldDefaults.colors(
            focusedBorderColor = borderColor,
            unfocusedBorderColor = borderColor.copy(alpha = 0.5f)
        )
    )
}

/**
 * 渐变边框输入框
 */
@Composable
fun GradientBorderTextField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    label: String? = null,
    placeholder: String? = null,
    gradientColors: List<Color> = listOf(
        MaterialTheme.colorScheme.primary,
        MaterialTheme.colorScheme.secondary
    ),
    shape: Shape = TextFieldShapes.medium,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    singleLine: Boolean = true,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE
) {
    // 注意：这里需要自定义绘制渐变边框，简化实现
    OutlinedTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        label = label?.let { { Text(it) } },
        placeholder = placeholder?.let { { Text(it) } },
        enabled = enabled,
        readOnly = readOnly,
        singleLine = singleLine,
        maxLines = maxLines,
        shape = shape,
        colors = OutlinedTextFieldDefaults.colors(
            focusedBorderColor = gradientColors.first(),
            unfocusedBorderColor = gradientColors.first().copy(alpha = 0.5f)
        )
    )
}

/**
 * 样式预览组件
 */
@Preview(showBackground = true)
@Composable
fun TextFieldStylesPreview() {
    MaterialTheme {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            var text by remember { mutableStateOf("示例文本") }
            
            Text(
                text = "输入框样式示例",
                style = MaterialTheme.typography.headlineSmall,
                fontWeight = FontWeight.Bold
            )
            
            // 不同样式
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "默认样式",
                style = TextFieldStyle.Default
            )
            
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "主要样式",
                style = TextFieldStyle.Primary
            )
            
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "成功样式",
                style = TextFieldStyle.Success
            )
            
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "警告样式",
                style = TextFieldStyle.Warning
            )
            
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "错误样式",
                style = TextFieldStyle.Error
            )
            
            // 不同尺寸
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                StyledTextField(
                    value = text,
                    onValueChange = { text = it },
                    label = "小",
                    size = TextFieldSize.Small,
                    modifier = Modifier.weight(1f)
                )
                
                StyledTextField(
                    value = text,
                    onValueChange = { text = it },
                    label = "中",
                    size = TextFieldSize.Medium,
                    modifier = Modifier.weight(1f)
                )
                
                StyledTextField(
                    value = text,
                    onValueChange = { text = it },
                    label = "大",
                    size = TextFieldSize.Large,
                    modifier = Modifier.weight(1f)
                )
            }
            
            // 不同形状
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "胶囊形状",
                shape = TextFieldShapes.pill
            )
            
            StyledTextField(
                value = text,
                onValueChange = { text = it },
                label = "大圆角",
                shape = TextFieldShapes.large
            )
        }
    }
}